<template>
	<view>
		<view class="bottom-car-box">
			<view class="car-box">
				<view class="ico" @click="showCarPoup=true">
					<image src="/static/index/car-ico.png"></image>
					<view class="badge-box">
						<u-badge bgColor="#FF0000"  max="99" :value="cart_total_num"></u-badge>
					</view>
				</view>
				<view class="car-box-left">
					<text>购物车</text>
				</view>
				<view class="car-box-right" @click="toPayment()">
					去结算
				</view>
			</view>
		</view>
		
		<!-- 购物车列表弹框 -->
		<u-popup :show="showCarPoup" round="16" mode="bottom" @close="showCarPoup=false" @open="showCarPoup=true">
			<scroll-view  catchtouchmove="true" class="poup-car" scroll-y="true">
				<view class="">
						<view class="poup-cat-top">
							<text>购物车</text>
							<view class="cat-top-right" @click="deleteAllCar">
								<image src="/static/common/delete.png"></image>
								<text>清空购物车</text>
							</view>
						</view>
						<view class="car-goods-list">
							<view class="car-goods-item" v-for="(item,index) in cart_products" :key="index">
								<view class="car-goods-img">
									<image :src="item.image" mode="aspectFill"></image>
								</view>
								<view class="car-goods-info">
									<view class="car-goods-title ellipsis-1">
										{{item.title||''}}
									</view>
									<view class="car-goods-bot">
										<text>{{item.priceSale||''}}</text>
										<view class="car-num">
											<u-number-box :min="0"  v-model="item.num" :max="item.kucun"  @change="(e) => updateCarNum(e, item,index)"></u-number-box>
										</view>
									</view>
								</view>
							</view>
						</view>
				</view>
			</scroll-view>
			
		</u-popup>
		
	</view>
</template>

<script>
	export default {
		props:{
		},
		data() {
			return {
				showCarPoup:false,
				cart_is_selected_all: false,
				cart_products: [], //产品列表
				cart_total_num: 0, //购物车总数量
				cart_total_price: 0, //购物车总金额
				SelectCarList:[],//点击加入购物车的数据
			};
		},
		methods:{
			init(list,totalnum){
				this.cart_products=list
				this.cart_total_num=totalnum
			},
			deleteAllCar(){
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'gouwuche_delAll',
					},
				}).then(res => {
					this.$alert(res)
					this.cart_products=[] //产品列表
					this.cart_total_num=0 //购物车总数量
					this.cart_total_price=0 //购物车总金额
					this.SelectCarList=[]
					this.$emit('delAll')
				})
			},
			
			updateCarNum(e,item,index){ //type==1是产品列表的加入购物车 2是购物车列表里的值
				this.$log('item', item)
				console.log('e', e)
				let num = e.value;
				if(num==0){ //删除此商品
					this.doDeleteSelect(item,index)
					return false;
				}
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'gouwuche_updateNum',
						inventoryId: item.inventoryId, //产品规格id(多个用,分开)
						num: num,
					},
				}).then(res => {
					if (res.code == 200) {
						this.$set(this.cart_products[index],'num',num)
						this.cart_total_num=res.data.count
						// this.cart_products = this.cart_products.filter(v => !v.is_selected);
						this.$store.commit('set_vuex_cart_number', res.data.count)
					}
				})
			},
			
			
			
			doDeleteSelect(item,index) {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'gouwuche_del',
						inventoryId: item.inventoryId, //产品规格id(多个用,分开)
					},
				}).then(res => {
					if (res.code == 200) {
						console.log(index,'第几个')
						this.cart_products.splice(index,1)
						this.cart_total_num=res.data.count
						this.$store.commit('set_vuex_cart_number', res.data.count)
					}
				})
			},
			
			
			toPayment() {
				if(this.cart_products && this.cart_products.length==0){
					this.$alert('请加购商品')
					return;
				}
				let items = this.cart_products.map(v => ({
					title: v.title,
					poster: v.image,
					guige_value: v.keyVals,
					guige_id: v.inventoryId,
					productId: v.productId,
					num: v.num,
					priceMarket: v.priceMarket || 0,
					priceSale: v.priceSale || 0,
				}));
				this.$store.commit('set_cache_payment_products', JSON.stringify(items))
			
				this.$log('待下单产品信息', items)
				// return 
				this.toRoute({
					path: '/order-submit',
					query: {
						from: 'cart'
					}
				})
			},
			
		}
	}
</script>

<style lang="scss">
.bottom-car-box{
		position: fixed;
		bottom: 0;
		z-index: 1000000;
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0rpx -8rpx 20rpx 2rpx rgba(0,0,0,0.06);
			.car-box{
				position: relative;
				display: flex;
				align-items: center;
				padding:0 32rpx;
					.ico{
						position: absolute;
							image{
								width: 139rpx;
								height: 128rpx;
							}
							.badge-box{
								position: absolute;
								top:14rpx;
								left: 110rpx;
								z-index: 10000;
							}
					}
					.car-box-left{
						flex:1;
						height: 80rpx;
						line-height: 80rpx;
						background-color: #000000;
						border-radius: 40rpx 0rpx 0rpx 40rpx;
						padding-left: 162rpx;
							text{
								font-weight: bold;
								font-size: 32rpx;
								color: #FFFFFF;
							}
					}
					.car-box-right{
						width: 160rpx;
						height: 80rpx;
						line-height: 80rpx;
						background: #CB0011;
						border-radius: 0rpx 40rpx 40rpx 0rpx;
						font-size: 28rpx;
						color: #FFFFFF;
						text-align: center;
					}
			}
			
	}
	.poup-car{
		padding: 34rpx 32rpx;
		height:908rpx;
				.poup-cat-top{
					display: flex;
					align-items: center;
					justify-content: space-between;
						text{
							font-weight: bold;
							font-size: 24rpx;
							color: #000000;
						}
						.cat-top-right{
							display: flex;
							align-items: center;
							image{
								width: 24rpx;
								height:24rpx;
								margin-right: 4rpx;
							}
						}
				}
				.car-goods-list{
					.car-goods-item{
						display: flex;
						padding: 40rpx 0 32rpx;
						border-bottom: 2rpx solid #DCE2E8;
						.car-goods-img{
							width: 124rpx;
							height: 124rpx;
							image{
								width: 100%;
								height: 100%;
							}
						}
						.car-goods-info{
							width:562rpx;
							margin-left: 24rpx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
								.car-goods-title{
									font-weight: bold;
									font-size: 32rpx;
									color: #000000;
								}
								.car-goods-bot{
									display: flex;
									align-items: center;
									justify-content: space-between;
									position: relative;
										>text{
											padding-left: 20rpx;
											&:before{
												content: "￥";
												position: absolute;
												left:0;
												font-weight: bold;
												font-size: 20rpx;
												color: #000000;
												top:50%;
												transform: translateY(-50%);
											}
										}
								}
						}
					}
				}
	}
	/deep/.u-number-box__minus{
		width: 36rpx !important;
		height: 36rpx !important;
		background: #FFFFFF;
		border-radius: 22rpx 22rpx 22rpx 22rpx;
		border: 2rpx solid #CB0011;
	}
	/deep/.u-icon__icon{
		font-size: 20rpx !important;
		color:#000000 !important;
	}
	/deep/.u-number-box__input{
		background-color: transparent !important;
		min-width: 26rpx !important;
	}
	/deep/.u-number-box__plus{
		width: 36rpx !important;
		height: 36rpx !important;
		background: #CB0011 !important;
		border-radius: 22rpx 22rpx 22rpx 22rpx;
	}
	/deep/.u-number-box__plus .u-icon__icon{
			font-size: 20rpx !important;
			color:#FFFFFF !important;
	}
	/deep/.u-number-box__minus{
		border-top-left-radius: 22rpx !important;
		border-bottom-left-radius: 22rpx !important;
	}
	/deep/.u-number-box__plus{
		border-top-right-radius: 22rpx !important;
		border-bottom-right-radius: 22rpx !important;
	}
</style>
